﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model_SSResourceManager
{
    public class UserDataContext : ItemDataContext <User,USER>
    {
        public UserDataContext()
        {
            EntitySetName = "USERS";
        }

        public List<User> GetActiveUsers()
        {
            List<User> _retList = new List<User>();

            using (SSRMDBEntities context = new SSRMDBEntities())
            {
                try
                {                   
                    User modelItem;
                    foreach (USER entItem in (from entUser in context.USERS 
                                              where entUser.Status == (byte)UserStatus.Active  select entUser  ) 
                            )
                       {
                           modelItem = new User();
                           EntityToModel(entItem, modelItem);
                           _retList.Add(modelItem);
                       }
                }
                catch (Exception ex)
                {
                    OnRaiceDbErrEvent(new DbErrEventArgs(ex.Message));
                }
            }

            return _retList;
        }

        protected override void EntityToModel(USER _entUser, User _user)
        {
            _user.Id        =   _entUser.Id;
            _user.FirstName =   _entUser.FirstName;
            _user.LastName  =   _entUser.LastName;
            _user.Login     =   _entUser.Login;
            _user.Password  =   _entUser.Password;
            _user.Email     =   _entUser.Email;
            _user.Skype     =   _entUser.Skype;
            _user.Role      =   (UserRoles)_entUser.Role;
            _user.Status    =   (UserStatus)_entUser.Status;
            _user.MessageTimeSpan = _entUser.MsgTimeSpan;
            _user.Online    = _entUser.OnLine;
        }

        protected override void ModelToEntity(User _user, USER _entUser)
        {
            _entUser.Id = _user.Id;
            _entUser.FirstName = _user.FirstName;
            _entUser.LastName = _user.LastName;
            _entUser.Login = _user.Login;
            _entUser.Password = _user.Password;
            _entUser.Email = _user.Email;
            _entUser.Skype = _user.Skype;
            _entUser.Role = (byte)_user.Role;
            _entUser.Status = (byte)_user.Status;
            _entUser.MsgTimeSpan  = _user.MessageTimeSpan ;
            _entUser.OnLine = _user.Online;
        }
    }
}
